package sisdroid.client.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DemoDbAdapter
{
    public static final String KEY_TYPE		= "value";
    public static final String KEY_VALUE	= "type";
    public static final String KEY_ROWID	= "_id";
    public static final String KEY_FROMID	= "fromid";
    public static final String KEY_TOID		= "toid";
    
    
    private static final String TAG = "DemoDbAdapter";
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;
    
    /**
     * Database creation sql statement
     */
    private static final String[] DATABASE_CREATE = {
            "create table nets (_id integer primary key autoincrement, "
                    + "type text not null, value text not null);",
                    "create table links (_id integer primary key autoincrement, "
                    + "fromid integer not null, toid integer not null);" };

    private static final String DATABASE_NAME = "SisSniffer";
    private static final String DATABASE_TABLE_NETS = "nets";
    private static final String DATABASE_TABLE_LINKS = "links";
    private static final int DATABASE_VERSION = 4;

    private final Context mCtx;

    private static class DatabaseHelper extends SQLiteOpenHelper
    {

        DatabaseHelper(Context context)
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db)
        {
        	for ( String stmt : DATABASE_CREATE)
        		db.execSQL( stmt );
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_NETS );
            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_LINKS );
            onCreate(db);
        }
    }

    /**
     * Constructor - takes the context to allow the database to be
     * opened/created
     * 
     * @param ctx the Context within which to workx
     */
    public DemoDbAdapter(Context ctx)
    {
        this.mCtx = ctx;
    }

    /**
     * Open the database. If it cannot be opened, try to create a new
     * instance of the database. If it cannot be created, throw an exception to
     * signal the failure
     * 
     * @return this (self reference, allowing this to be chained in an
     *         initialization call)
     * @throws SQLException if the database could be neither opened or created
     */
    public DemoDbAdapter open() throws SQLException
    {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }
    
    public void close()
    {
        mDbHelper.close();
    }


    public long createNet( sisdroid.client.model.Location net )
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_TYPE, net.getType() );
        initialValues.put(KEY_VALUE, net.getValue() );

        
        
        return mDb.insert(DATABASE_TABLE_NETS, null, initialValues);
    }
    
    public long createLink( long from, long to )
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_FROMID, from );
        initialValues.put(KEY_TOID, to );

        return mDb.insert(DATABASE_TABLE_LINKS, null, initialValues);
    }


    public boolean deleteNet( long rowId )
    {

        return mDb.delete(DATABASE_TABLE_NETS, KEY_ROWID + "=" + rowId, null) > 0;
    }

    /**
     * Return a Cursor over the list of all notes in the database
     * 
     * @return Cursor over all notes
     */
    public Cursor fetchAllNets()
    {
        return mDb.query(DATABASE_TABLE_LINKS, new String[] {KEY_ROWID, KEY_TYPE,
                KEY_VALUE}, null, null, null, null, null);
    }

    /**
     * Return a Cursor positioned at the note that matches the given rowId
     * 
     * @param rowId id of note to retrieve
     * @return Cursor positioned to matching note, if found
     * @throws SQLException if note could not be found/retrieved
     */
    public Cursor fetchNet( long rowId ) throws SQLException {

        Cursor mCursor =

                mDb.query(true, DATABASE_TABLE_NETS, new String[] {KEY_ROWID,
                        KEY_TYPE, KEY_VALUE}, KEY_ROWID + "=" + rowId, null,
                        null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;

    }
}
